import 'package:enjoy_plus_hm/pages/house/building_list.dart';
import 'package:enjoy_plus_hm/pages/house/house_detail.dart';
import 'package:enjoy_plus_hm/pages/house/house_form.dart';
import 'package:enjoy_plus_hm/pages/house/house_list.dart';
import 'package:enjoy_plus_hm/pages/house/location_list.dart';
import 'package:enjoy_plus_hm/pages/house/room_list.dart';
import 'package:enjoy_plus_hm/pages/login/index.dart';
import 'package:enjoy_plus_hm/pages/notic/index.dart';
import 'package:enjoy_plus_hm/pages/profile/index.dart';
import 'package:enjoy_plus_hm/pages/tab_bar_page.dart';
import 'package:enjoy_plus_hm/stores/counter.dart';
import 'package:enjoy_plus_hm/utils/token.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) => CounterModel()),
    ],
    child: MaterialApp(
      routes: {
        '/': (context) => const TabBarPage(),
        '/detail': (context) => const NoticeDetail()
      },
      onGenerateRoute: (settings) {
        final tokenManager = TokenManager();
        final token = tokenManager.getToken() ?? '';
        if (token.isEmpty && settings.name != '/login') {
          return MaterialPageRoute(builder: (context) => const LoginPage());
        }

        if (settings.name == '/profile') {
          return MaterialPageRoute(
              builder: (context) =>
                  ProfilePage(userInfo: settings.arguments as Map));
        }

        if (settings.name == '/house_list') {
          return MaterialPageRoute(builder: (context) => const HouseList());
        }

        //location_list
        if (settings.name == '/location_list') {
          return MaterialPageRoute(builder: (context) => const LocationList());
        }

        if (settings.name == '/building_list') {
          return MaterialPageRoute(
              builder: (context) =>
                  BuildingList(point: (settings.arguments as Map)['point']));
        }

        if (settings.name == '/room_list') {
          Map temp = settings.arguments as Map;
          return MaterialPageRoute(
              builder: (context) => RoomList(
                    point: temp['point'],
                    building: temp['building'],
                  ));
        }

        if (settings.name == '/house_form') {
          return MaterialPageRoute(
              builder: (context) => HouseForm(
                    params: settings.arguments as Map,
                  ));
        }

        if (settings.name == '/house_detail') {
          return MaterialPageRoute(
              builder: (context) =>
                  HouseDetail(id: (settings.arguments as Map)['id']));
        }

        return null;
      },
      initialRoute: '/',
    ),
  ));
}
